package org.eclipse.football;

import java.io.IOException;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class PickDefPlayerServlet extends HttpServlet implements Servlet {
	// Override doPost function
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// Read in information submitted by form
		String name = request.getParameter("name");
		
		// Obtain existing league
		League league = (League) request.getSession(true).getAttribute("league");
		
		// Obtain existing team
		Team team = (Team) request.getSession(true).getAttribute("team");
		
		// Initialize player to null
		DefPlayer player = null;
		
		// Make sure team is not null
		if(team != null){
			// If so look for a defensive player with the given name
			player = team.findDefPlayer(name);
		}
		
		// Write out the state of the league to a file
		league.writeState();
		
		// Set league in the session
		request.getSession().setAttribute("league", league);
		
		// Set the team in the session
		request.getSession().setAttribute("team", team);
		
		// Set player in the session
		request.getSession().setAttribute("def", player);
		
		// Set kicker and off as null in the session
		request.getSession().setAttribute("off", null);
		request.getSession().setAttribute("kicker", null);
		
		// Forward control back to players.jsp
		getServletContext().getRequestDispatcher("/stats.jsp").forward(request, response);
		
		// TODO Auto-generated method stub
		//super.doPost(request, response);
	}

}

